/**
 * Created by yuzhang on 2018/4/26.
 */

import {guid, formatDate} from './utils'

export function normal (type, name, logLot, data) {
  if (!type || !name) {
    throw new Error('Type and Name should not be null!')
  }
  return {
    id: data.id || guid(),
    eventType: type,
    eventName: name,
    eventData: data,
    logLot: logLot,
    eventTime: data.startTime || formatDate(new Date())
  }
}

export function func (name, logLot, message, data) {
  return normal('function', name, logLot, {message, ...data})
}

export function duration (name, logLot, duration, data) {
  return normal('duration', name, logLot, {duration, ...data})
}

export function background (name, logLot, duration, data) {
  return normal('background', name, logLot, {duration, ...data})
}

// 新增一个区分页面加载时长的方法
export function loading (duration, data) {
  // return normal('loading', name, logLot, {duration, ...data})
  return {
    id: data.id || guid(),
    ...data,
    duration: duration
  }
}

/**
 *
 * @param type 'remember|conventional(登录类型：记住密码|常规)'
 * @param userID
 * @returns {{id: *, userID: (string), loginType: *, loginTime: Date}}
 */
export function loginSuccess (type, userID) {
  // return normal('login', 'loginSuccess', {type, ...data})
  return {
    id: guid(),
    userID,
    logLot: guid(),
    loginType: type,
    loginTime: formatDate(new Date())
  }
}
